package com.qkl.xiatang.app.utils;

import org.apache.commons.lang3.StringUtils;

import java.sql.Time;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;

public class AppDateUtils {

    public static final String YYYY_MM_DD = "yyyy-MM-dd";

    public static final String YYYYMMDD = "yyyy.MM.dd";

    public static Date yyyymmddTodate(String dateString){
        return parseDate(new SimpleDateFormat(YYYYMMDD),dateString);
    }

    /**
     *
     * @param dateString yyyy-MM-dd
     * @return
     */
    public static Date parseYyyy_mm_dd(String dateString){
        return parseDate(new SimpleDateFormat(YYYY_MM_DD),dateString);
    }

    /**
     *
     * @param date yyyy-MM-dd
     * @return
     */
    public static boolean checkDate(String date){
        String time = date;
        if(StringUtils.isAnyEmpty(time)){
            return false;
        }
        time = AppUtils.toYYYY_MM_DD(time);
        if(!AppUtils.checkYyyyMMdd(time)){
            return false;
        }
        return true;
    }

    public static Date todayYyyyMMdd(){
        SimpleDateFormat format = new SimpleDateFormat(YYYY_MM_DD);
        return parseDate(format,format.format(new Date()));
    }

    public static Date parseDate(SimpleDateFormat format,String dateString){
        try {
            return format.parse(dateString);
        }catch (Exception e){
            return null;
        }
    }


    public static Date todayDate(Integer hour){
        if(null != hour && hour >= 0 && hour < 24){
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(todayYyyyMMdd());
            calendar.add(Calendar.HOUR_OF_DAY,hour);
            return calendar.getTime();
        }
        return null;
    }

    public static boolean betweenEndHour(Integer endHour,int queryCycleMinute){
        Calendar calendar2 = Calendar.getInstance();
        int nowHour = calendar2.get(Calendar.HOUR_OF_DAY);
        long nowTime = System.currentTimeMillis();
        return betweenEndHour(nowHour,nowTime,endHour,queryCycleMinute);
    }

    public static boolean betweenEndHour(int nowHour ,long nowTime,Integer endHour,int queryCycleMinute){
        Date endTime = todayDate(endHour);
        if(null == endHour || endHour > nowHour){
            return  false;
        }
        if((nowTime - endTime.getTime()) > queryCycleMinute * 60 * 1000){
            return  false;
        }
        return  true;
    }

    public static boolean betweenEndDate(long nowTime,Date endTime,int queryCycleMinute){
       return betweenEndDate(nowTime,endTime.getTime(),queryCycleMinute);
    }

    private static boolean betweenEndDate(long nowTime,long endTime,int queryCycleMinute){
        if(nowTime < endTime){
            return  false;
        }
        if((nowTime - endTime) > queryCycleMinute * 60 * 1000){
            return  false;
        }
        return  true;
    }

    public static String formatMsgDate(Date msgDate){
        if(null == msgDate){
            return "";
        }
        return new SimpleDateFormat("yyyy.MM.dd").format(msgDate);
    }

    public static LocalDateTime toLocalDateTime(Date date){
        if(null == date){
            return null;
        }
        Instant instant = date.toInstant();
        return LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
    }

    public static LocalDate toLocalDate(Date date){
        LocalDateTime localDateTime = toLocalDateTime(date);
        if(null == localDateTime){
            return null;
        }
        return  localDateTime.toLocalDate();
    }

    public static long between(Date beginDate ,Date endDate){
        return between(toLocalDate(beginDate),toLocalDate(endDate));
    }

    public static long between(LocalDate beginDate ,LocalDate endDate){
        return ChronoUnit.DAYS.between(beginDate,endDate);
    }

    public static Date addDaysForToday(int days){
        LocalDate localDate = LocalDate.now().plusDays(days);
       return Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
    }

    public static Date toFeedDate(Date allocDate,Integer hour){
        if(null != hour && hour >= 0 && hour < 24){
            return toFeedDate(allocDate,LocalTime.of(hour,0,0));
        }
        return null;
    }

    public static Date toFeedDate(Date allocDate,Time time){
        return toFeedDate(allocDate,time.toLocalTime());
    }

    public static Date addDays(Date date,int days){
        LocalDateTime localDateTime = toLocalDateTime(date).plusDays(days);
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    public static Date addHours(Date date,int hours){
        LocalDateTime localDateTime = toLocalDateTime(date).plusHours(hours);
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    private static Date toFeedDate(Date allocDate,LocalTime localTime){
        LocalDateTime localDateTime = LocalDateTime.of(toLocalDate(allocDate), localTime);
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }


    public static void main(String[] args) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss:SSS");
        Date date = new Date();

        System.out.println(format.format(addDays(date,1)));
        System.out.println(format.format(addHours(date,1)));
        System.out.println(format.format(date));
    }


}
